package com.deyuanyun.pic.settings.service.base.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.deyuanyun.pic.common.util.StringUtils;
import com.deyuanyun.pic.settings.domain.base.BizMsgEvent;
import com.deyuanyun.pic.settings.domain.base.BizMsgEventExample;
import com.deyuanyun.pic.settings.enumeration.MsgEvent;
import com.deyuanyun.pic.settings.mapper.base.BizMsgEventMapper;
import com.deyuanyun.pic.settings.service.base.BizMsgEventService;
import com.deyuanyun.pic.settings.service.msgtemplate.AbstractPushMsgTemplate;
import com.deyuanyun.pic.settings.service.msgtemplate.InspAppraisePushMsg;
import com.deyuanyun.pic.settings.service.msgtemplate.InspResultInputPushMsg;
import com.deyuanyun.pic.settings.service.msgtemplate.RiskSystemPushMsg;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *  业务事件信息service实现类
 *  @author axiao(xj08.dream@gmail.com)
 *  @date 2016-07-22
 *  @version 1.0
 *  Histroy: [修改人][时间][描述]
 */
@Service
public class BizMsgEventServiceImpl implements BizMsgEventService {

    private static final Logger LOGGER = LoggerFactory.getLogger(BizMsgEventServiceImpl.class);

    @Autowired
    private BizMsgEventMapper bizMsgEventMapper;

    @Override
    public void chkRiskInputFail() {
        if(LOGGER.isInfoEnabled())
            LOGGER.info("开始查询失败的风险评价数据录入");
        BizMsgEventExample example = new BizMsgEventExample();
        example.setDistinct(false);
        example.createCriteria().andDeletedEqualTo((short)0).
                andTypeEqualTo(MsgEvent.RISK_INPUT.toString()).andStatusEqualTo((short)0);
        List<BizMsgEvent> bizMsgEvents = bizMsgEventMapper.selectByExampleWithBLOBs(example);
        if(bizMsgEvents != null && bizMsgEvents.size() > 0) {
            if(LOGGER.isInfoEnabled()) {
                LOGGER.info("查询到失败的风险评价数据有：" + bizMsgEvents.size() + "条");
            }
            for (BizMsgEvent bizMsgEvent : bizMsgEvents) {
                try {
                    String content = new String(bizMsgEvent.getContent(), "UTF-8");

                    if (StringUtils.isNotEmpty(content)) {
                        JSONObject jsonObject = JSON.parseObject(content);
                        String codes = jsonObject.getString("codeList");
                        if (StringUtils.isNotEmpty(codes)) {
                            String[] codeArr = codes.split("#");
                            if (codeArr != null && codeArr.length > 0) {
                                AbstractPushMsgTemplate riskSystemPushMsg  = new RiskSystemPushMsg(); //风险评价页面-----风险等级相关消息推送模板
                                Map map = new HashMap();
                                map.put(RiskSystemPushMsg.CODE_LIST_KEY, Arrays.asList(codeArr));
                                riskSystemPushMsg.setInterface(true, map);
                                riskSystemPushMsg.pushMsg();
                                bizMsgEvent.setStatus((short) 1);
                                bizMsgEvent.setDeleted((short) 1);
                                bizMsgEventMapper.updateByPrimaryKeySelective(bizMsgEvent);
                                if(LOGGER.isInfoEnabled()) {
                                    LOGGER.info("已处理id为：" + bizMsgEvent.getId() + "，类型为：" + bizMsgEvent.getType() + "的数据");
                                }
                            }
                        }
                    }
                } catch (Exception e) {
                    LOGGER.error("执行异常", e);
                }
            }
        }
        if(LOGGER.isInfoEnabled())
            LOGGER.info("处理失败的风险评价数据结束");
    }

    @Override
    public void chkInspAppraiseInputFail() {
        try {
            if(LOGGER.isInfoEnabled())
                LOGGER.info("开始查询失败的内检测评价新增数据录入");
            BizMsgEventExample example = new BizMsgEventExample();
            example.setDistinct(false);
            example.createCriteria().andDeletedEqualTo((short)0).
                    andTypeEqualTo(MsgEvent.INSP_APPRAISE_INPUT.toString()).andStatusEqualTo((short)0);
            List<BizMsgEvent> bizMsgEvents = bizMsgEventMapper.selectByExampleWithBLOBs(example);
            if(bizMsgEvents != null && bizMsgEvents.size() > 0) {
                if (LOGGER.isInfoEnabled()) {
                    LOGGER.info("查询到失败的内检测评价更新数据有：" + bizMsgEvents.size() + "条");
                }
                for (BizMsgEvent bizMsgEvent : bizMsgEvents) {
                    String content = new String(bizMsgEvent.getContent(), "UTF-8");
                    if (StringUtils.isNotEmpty(content)) {
                        JSONObject jsonObject = JSON.parseObject(content);
                        AbstractPushMsgTemplate inspAppraiseCPushMsg = new InspAppraisePushMsg();
                        inspAppraiseCPushMsg.setInterface(true, jsonObject);
                        inspAppraiseCPushMsg.pushMsg();
                        bizMsgEvent.setStatus((short) 1);
                        bizMsgEvent.setDeleted((short) 1);
                        bizMsgEventMapper.updateByPrimaryKeySelective(bizMsgEvent);
                        if (LOGGER.isInfoEnabled()) {
                            LOGGER.info("已处理id为：" + bizMsgEvent.getId() + "，类型为：" + bizMsgEvent.getType() + "的数据");
                        }
                    }
                }
                if (LOGGER.isInfoEnabled())
                    LOGGER.info("处理失败的内检测评价更新数据结束");
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public void chkInspResultInputFail() {
        if(LOGGER.isInfoEnabled())
            LOGGER.info("开始查询失败的内检测数据录入");
        BizMsgEventExample example = new BizMsgEventExample();
        example.setDistinct(false);
        example.createCriteria().andDeletedEqualTo((short)0).
                andTypeEqualTo(MsgEvent.INSP_RESULT_INPUT.toString()).andStatusEqualTo((short)0);
        List<BizMsgEvent> bizMsgEvents = bizMsgEventMapper.selectByExampleWithBLOBs(example);
        if(bizMsgEvents != null && bizMsgEvents.size() > 0) {
            if(LOGGER.isInfoEnabled()) {
                LOGGER.info("查询到失败的内检测数据有：" + bizMsgEvents.size() + "条");
            }
            for (BizMsgEvent bizMsgEvent : bizMsgEvents) {
                try {
                    String content = new String(bizMsgEvent.getContent(), "UTF-8");
                    if (StringUtils.isNotEmpty(content)) {
                        JSONObject jsonObject = JSON.parseObject(content);
                        AbstractPushMsgTemplate inspResultInputPushMsg  = new InspResultInputPushMsg(); //完整性评价页面-----内检测结果录入后推送模板
                        inspResultInputPushMsg.setInterface(true, jsonObject);
                        inspResultInputPushMsg.pushMsg();//推送消息
                        bizMsgEvent.setStatus((short) 1);
                        bizMsgEvent.setDeleted((short) 1);
                        bizMsgEventMapper.updateByPrimaryKeySelective(bizMsgEvent);//把改条记录更改为已处理
                        if(LOGGER.isInfoEnabled()) {
                            LOGGER.info("已处理id为：" + bizMsgEvent.getId() + "，类型为：" + bizMsgEvent.getType() + "的数据");
                        }
                    }
                } catch (Exception e) {
                    LOGGER.error("执行出现异常");
                    throw new RuntimeException(e);
                }
            }
        }
        if(LOGGER.isInfoEnabled())
            LOGGER.info("处理失败的内检测数据录入结束");
    }
}
